// pages/player/player.js
let musiclist = []
// 正在播放音乐的Index值
let nowPlayingIndex = 0
// 获取全局位移的背景音频管理器
const backgroundAudioManager = wx.getBackgroundAudioManager()
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    picUrl: '',
    isplaying: false, //false表示不播放，true表示播放
    isLyricShow: false, //表示歌词是否显示
    lyric: '',
    isSame: false //是不是同一首歌
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options)
    // 当前点击的歌曲index
    nowPlayingIndex = options.index
    // 获取Storage的歌曲列表信息
    musiclist = wx.getStorageSync('musiclist')
    // console.log(musiclist)
    this._LoadMusicDetail(options.musicId)

  },
  _LoadMusicDetail(musicId) {
    if (musicId == app.getPlayMusicId()) {
      this.setData({
        isSame: true
      })
    } else {
      this.setData({
        isSame: false
      })
    }
    if (!this.data.isSame) {
      backgroundAudioManager.stop()
    }

    // 存储点击的歌曲信息
    let music = musiclist[nowPlayingIndex]
    // console.log(music)
    wx.setNavigationBarTitle({
        title: music.name,
      }),
      this.setData({
        picUrl: music.al.picUrl
      }),

      app.setPlayMusicId(musicId)

    wx.showLoading({
      title: '加载中',
    })
    // console.log(this.data.picUrl)
    wx.cloud.callFunction({
      name: 'music',
      data: {
        musicId,
        $url: 'musicUrl',
      }
    }).then(res => {
      console.log(JSON.parse(res.result))
      let result = JSON.parse(res.result)
      if (result.data[0].url==null){
        wx.showToast({
          title: '无权限',
        })
        return
      }
      if (!this.data.isSame) {
        backgroundAudioManager.src = result.data[0].url
        // backgroundAudioManager.src = `https://music.163.com/song/media/outer/url?id=${music.id}.mp3 `
        backgroundAudioManager.title = music.name
        backgroundAudioManager.coverImgUrl = music.picUrl
        backgroundAudioManager.singer = music.ar[0].name
        backgroundAudioManager.epname = music.al.name
      }
      this.setData({
        isplaying: true
      })
      wx.hideLoading()


      //加载歌词
      wx.cloud.callFunction({
        name: 'music',
        data: {
          musicId,
          $url: 'lyric',
        }
      }).then(res => {
        // console.log(JSON.parse(res.result).lrc.lyric)
        let lyric = "无歌词"
        const lrc = JSON.parse(res.result).lrc
        if (lrc) {
          lyric = lrc.lyric
        }
        this.setData({
          lyric
        })
      })
    })
  },
  togglePlaying() {
    if (this.data.isplaying) {
      backgroundAudioManager.pause()
    } else {
      backgroundAudioManager.play()
    }
    this.setData({
      isplaying: !this.data.isplaying
    })
  },
  onPrev() {
    nowPlayingIndex--
    if (nowPlayingIndex < 0) {
      nowPlayingIndex = musiclist.length - 1
    }
    this._LoadMusicDetail(musiclist[nowPlayingIndex].id)
  },
  onNext() {
    nowPlayingIndex++
    if (nowPlayingIndex === musiclist.length) {
      nowPlayingIndex = 0
    }
    this._LoadMusicDetail(musiclist[nowPlayingIndex].id)
  },
  onChangeLyric() {
    this.setData({
      isLyricShow: !this.data.isLyricShow
    })
  },
  timeUpdate(event) {
    this.selectComponent('.Lyric').update(event.detail.currentTime)
  },
  onPlay() {
    this.setData({
      isplaying: true
    })
  },
  onPause() {
    this.setData({
      isplaying: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})